<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/gltf_logo.png"/>
    <h1>GLTFLoaderPlugin</h1>
    <h2>Loading and Transforming Models</h2>
    <h3>Stats</h3>
    <ul>
        <li>
            <div id="time">Loading JavaScript modules...</div>
        </li>
    </ul>
    <h3>Components used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/GLTFLoaderPlugin/GLTFLoaderPlugin.js~GLTFLoaderPlugin.html"
               target="_other">GLTFLoaderPlugin</a>

        </li>
        <li>
            <a href="../../docs/class/src/plugins/TreeViewPlugin/TreeViewPlugin.js~TreeViewPlugin.html"
               target="_other">TreeViewPlugin</a>
        </li>
    </ul>
    <h3>Assets</h3>
    <ul>
        <li>
            <a href="http://openifcmodel.cs.auckland.ac.nz/Model/Details/274"
               target="_other">Model source</a>
        </li>
    </ul>
</div>
</body>
<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, GLTFLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer, arrange the camera
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true
    });

    viewer.camera.eye = [-5.13, 16.83, 39.46];
    viewer.camera.look = [22.20, 1.86, 4.44];
    viewer.camera.up = [0.19, 0.94, -0.25];

    //----------------------------------------------------------------------------------------------------------------------
    // Create a xeokit loader plugin, load a model, fit to view
    //----------------------------------------------------------------------------------------------------------------------

    const glTFLoader = new GLTFLoaderPlugin(viewer, {});

    var t0 = performance.now();

    document.getElementById("time").innerHTML = "Loading models...";

    var i = 0;

    glTFLoader.load({
        src: "../../assets/models/gltf/Duplex/glTF-Embedded/Duplex.gltf",
        metaModelSrc: "../../assets/models/gltf/Duplex/Duplex.json",
        edges: true,
        scale: [0.5, 0.5, 0.5],
        position: [i++ * 10, 0, 0]
    })
        .on("loaded", () => {

            glTFLoader.load({
                src: "../../assets/models/gltf/Duplex/glTF-Embedded/Duplex.gltf",
                metaModelSrc: "../../assets/models/gltf/Duplex/Duplex.json",
                edges: true,
                scale: [0.5, 0.5, 0.5],
                position: [i++ * 10, 0, 0]
            })
                .on("loaded", () => {

                    glTFLoader.load({
                        src: "../../assets/models/gltf/Duplex/glTF-Embedded/Duplex.gltf",
                        metaModelSrc: "../../assets/models/gltf/Duplex/Duplex.json",
                        edges: true,
                        scale: [0.5, 0.5, 0.5],
                        position: [i++ * 10, 0, 0]
                    })
                        .on("loaded", () => {

                            glTFLoader.load({
                                src: "../../assets/models/gltf/Duplex/glTF-Embedded/Duplex.gltf",
                                metaModelSrc: "../../assets/models/gltf/Duplex/Duplex.json",
                                edges: true,
                                scale: [0.5, 0.5, 0.5],
                                position: [i++ * 10, 0, 0]
                            })
                                .on("loaded", () => {

                                    glTFLoader.load({
                                        src: "../../assets/models/gltf/Duplex/glTF-Embedded/Duplex.gltf",
                                        metaModelSrc: "../../assets/models/gltf/Duplex/Duplex.json",
                                        edges: true,
                                        scale: [0.5, 0.5, 0.5],
                                        position: [i++ * 10, 0, 0]
                                    })
                                        .on("loaded", () => {
                                            var t1 = performance.now();
                                            document.getElementById("time").innerHTML = "Models loaded in " + Math.floor(t1 - t0) / 1000.0 + " seconds";
                                        });
                                });
                        });
                });
        });


    //------------------------------------------------------------------------------------------------------------------
    // Mouse over Entities to highlight them
    //------------------------------------------------------------------------------------------------------------------

    var lastEntity = null;

    viewer.scene.input.on("mousemove", function (coords) {

        var hit = viewer.scene.pick({
            canvasPos: coords
        });

        if (hit) {
            if (!lastEntity || hit.entity.id !== lastEntity.id) {
                if (lastEntity) {
                    lastEntity.highlighted = false;
                }
                lastEntity = hit.entity;
                hit.entity.highlighted = true;
            }
        } else {
            if (lastEntity) {
                lastEntity.highlighted = false;
                lastEntity = null;
            }
        }
    });

    window.viewer = viewer

</script>
</html>